/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.management.home.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tompai.common.enums.PermissionTypeEnums;
import com.tompai.common.model.SysPermission;
import com.tompai.common.util.EnvUtils;
import com.tompai.management.home.web.vo.SysPermissionMenuTreeVo;
import com.tompai.management.user.service.SysPermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 主页
 *
 * @author liinux@qq.com
 * @version 2019/10/7
 */
@Controller
@Api(tags = "首页管理")
@RequestMapping(value = "/manager")
public class HomeController {

	@Autowired
	private SysPermissionService sysPermissionService;

	@ApiOperation(value = "首页")
	@GetMapping("")
	@ApiIgnore
	public String index(Model model) {

		// 查询菜单列表
		List<SysPermission> menus = sysPermissionService.getByUserId(EnvUtils.getCurrentUserId());
		Map<String, SysPermissionMenuTreeVo> sysPermissionMenuTreeVoMap = menus.stream().filter(sysPermission -> {
			if (PermissionTypeEnums.MENU.getStringValue().equals(sysPermission.getType())) {
				return true;
			}
			return false;
		}).map(sysPermission -> {
			SysPermissionMenuTreeVo sysPermissionMenuTreeVo = new SysPermissionMenuTreeVo();
			sysPermissionMenuTreeVo.setId(sysPermission.getId());
			sysPermissionMenuTreeVo.setName(sysPermission.getName());
			sysPermissionMenuTreeVo.setUrl(sysPermission.getUrl());
			sysPermissionMenuTreeVo.setPid(sysPermission.getPid());
			sysPermissionMenuTreeVo.setChild(new ArrayList<>());
			return sysPermissionMenuTreeVo;
		}).collect(Collectors.toMap(SysPermissionMenuTreeVo::getId, a -> a));
		for (Map.Entry<String, SysPermissionMenuTreeVo> entry : sysPermissionMenuTreeVoMap.entrySet()) {
			SysPermissionMenuTreeVo value = entry.getValue();
			if (!"0".equals(value.getPid())) {
				sysPermissionMenuTreeVoMap.get(value.getPid()).getChild().add(value);
			}
		}
		List<SysPermissionMenuTreeVo> collect = sysPermissionMenuTreeVoMap.values().stream()
				.filter(sysPermissionMenuTreeVo -> {
					if (!"0".equals(sysPermissionMenuTreeVo.getPid())) {
						return false;
					} else {
						return true;
					}
				}).collect(Collectors.toList());
		model.addAttribute("menus", collect);
		model.addAttribute("userName", EnvUtils.getCurrentUserName());
		return "home/index";
	}

}
